﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Common;

/// <summary>
///enumtree 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class enumtree : System.Web.Services.WebService
{

    public enumtree()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }


    [WebMethod]
    public string EnumAdminTree(string enumDefId)
    {
        string HTML_TABLE = "<ul class=\"simpleTree\">{0}</ul>";
        string HTML_ITEMS = "<li class=\"simpleTree\" itemid=\"{0}\"><span class=\"simpleTree\"><a class=\"simpleTree\" href=\"javascript:mouseDown(this);\"><img class=\"simpleTree\" src=\"../_imgs/ico_Plus.gif\" alt=\"已折叠，单击即可展开\" /></a><input type=\"radio\" class=\"hiddenRadio\" name=\"subjectTree\" id=\"{0}\" /><label class=\"TreeItem\" for=\"{0}\">{1}</label></span></li>";

        string initEntityType = "EnumDef";
        if (enumDefId != "{00000000-0000-0000-0000-000000000000}")
        {
            object _obj = BLL.CommonBLL.ExecuteScalar("EnumDef", "COUNT(1)", " AND EnumDefId = '" + enumDefId + "'");
            if (_obj != null && (int)_obj > 0)
            {
                initEntityType = "EnumItem";
            }
            else
            {
                _obj = BLL.CommonBLL.ExecuteScalar("EnumItem", "COUNT(1)", " AND EnumItemId = '" + enumDefId + "'");
                if (_obj != null && (int)_obj > 0)
                {
                    return "";
                }
            }
        }

        DataTable dt = new DataTable();

        string strTreeNodeHTML = "";
        try
        {
            if (initEntityType == "EnumDef")
            {
                dt = BLL.CommonBLL.Select("EnumDef", " * ", " ORDER BY Title ASC ");
                if (dt == null || dt.Rows.Count <= 0)
                    return "";

                foreach (DataRow dr in dt.Select(""))
                {
                    strTreeNodeHTML += string.Format(HTML_ITEMS, dr["EnumDefId"].ToString(), dr["Title"].ToString());
                }
            }
            else
            {
                dt = BLL.CommonBLL.Select("EnumItem", " * ", " AND EnumKey IN (SELECT EnumKey FROM EnumDef WHERE EnumDefId = '" + enumDefId + "') ORDER BY SortIndex ASC ");
                if (dt == null || dt.Rows.Count <= 0)
                    return "";

                foreach (DataRow dr in dt.Select(""))
                {
                    strTreeNodeHTML += string.Format(HTML_ITEMS, dr["EnumItemId"].ToString(), dr["EnumText"].ToString());
                }
            }
            dt.Dispose();
        }
        catch
        { }
        finally
        {
            dt.Dispose();
        }
        string res = "";
        if (strTreeNodeHTML.Trim() != "")
        {
            res = string.Format(HTML_TABLE, strTreeNodeHTML);
        }
        return res;
    }

}

